﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using MrDev.Graphics;
using MrDev.Physics;

namespace MrDev.GameObjects
{
    class Platform : GameObject
    {
        private Animation animation;
        private Tile tile;
        private Vector2 startPosition;
        private Vector2 endPosition;
        private bool goToStart;
        private int size;

        public Platform(ContentManager content, Level level, Vector2 currentPosition, Vector2 startPosition, Vector2 endPosition, int size) : base(level)
        {
            this.Level = level;
            this.startPosition = startPosition;
            this.endPosition = endPosition;
            this.size = size;
            Parameters = PhysicsParameters.CreateEnemyParameters();

            SetColor(Color.Green);

            LoadContent();
            Level.Physics.SetObjectPosition(this, currentPosition);
            Parameters.SpriteSize = new Rectangle(0, 0, Tile.Width * size, Tile.Height);
        }

        public void LoadContent()
        {
            // Load animated textures.
            tile = new Tile(Level.Content.Load<Texture2D>(@"Tiles\tiles"), new Vector2(0, 0), TileCollision.Passable);
            animation = new Animation(Level.Content.Load<Texture2D>("Sprites/Enemy/Enemy"), 0.1f, true);
        }

        public override void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Vector2 destination = new Vector2(0, 0);
            Parameters.PreviousBounds = Level.Physics.AbsoluteCollisionRect(this);
            Parameters.PreviousPosition = Parameters.Position;
            if (Parameters.Position == startPosition || Parameters.Velocity == new Vector2(0, 0))
            {
                destination = endPosition - Parameters.Position;
                Vector2 destinationNormalized = destination;
                destinationNormalized.Normalize();
                Parameters.Velocity = 200f * destinationNormalized;
                goToStart = false;
            }
            if (Parameters.Position == endPosition)
            {
                destination = startPosition - Parameters.Position;
                Vector2 destinationNormalized = destination;
                destinationNormalized.Normalize();
                Parameters.Velocity = 200f * destinationNormalized;
                goToStart = true;
            }
            if (goToStart)
            {
                destination = startPosition - Parameters.Position;
            }
            else
            {
                destination = endPosition - Parameters.Position;
            }


            Vector2 moveDistance = this.Parameters.Velocity * elapsedTime;
            if (moveDistance.Length() > destination.Length())
            {
                moveDistance = destination;
            }

            Parameters.Position += moveDistance;
        }


        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < size; i++)
            {

                Color color = new Color(Color.White, Constants.Alpha);
                Vector2 pos = this.Parameters.Position;
                pos += new Vector2(-(float)this.Parameters.SpriteSize.Width / 2f + i * Tile.Width, -Tile.Height);
                pos = new Vector2((float)Math.Round(pos.X), (float)Math.Round(pos.Y));
                
                spriteBatch.Draw(tile.Texture, pos, tile.SourceRectangle, color);
            }

        }



    }
}
